import { RequestHandler } from 'express';

import fs from 'fs-extra';
import path from 'path';
import { API_REQUEST_START_TIME } from '../../../../../../../../../../global/api_config';
import { cur_path_center } from '../DataDefine/sourceData';
// import { mongoDB } from '../../../../../../../../../../conect_mongoDB_class_laugh_node';
import { create_uni_item_id } from '../Func/create_uni_item_id';
import { pgSQL } from '../../../../../../../../../../conect_postgresSQL_class_laugh_node';
import { path_file_user_prefix } from '../../../../../../../DataDefine/file_path';

const post_create_new_page_main_uni_comps: RequestHandler = (req, res) => {
  let uni_comps_info: page_main_uni_comps = req.body;

  let request_start_time = req.headers[API_REQUEST_START_TIME];
  console.log(uni_comps_info, request_start_time);

  create_uni_item_id(uni_comps_info);

  let cur_user_id = uni_comps_info.id;

  let file_name = `${uni_comps_info.id}_${request_start_time}.json`;

  let file_path = `${path_file_user_prefix}/${cur_user_id}/${cur_path_center}/${file_name}`;

  fs.outputFile(file_path, JSON.stringify(uni_comps_info));

  // const file_test = mongoDB.collection('test_mongo_cl');

  // file_test.insertOne(uni_comps_info);

  pgSQL.query('insert into users (name,phone) values ($1,$2)', [
    '张三',
    '123456',
  ]);

  res.send({ describe: 'create done', uni_comps_id: uni_comps_info.id });
};

export { post_create_new_page_main_uni_comps };
